package coroutines.coroutinescope.exception

import kotlinx.coroutines.*

class ExceptionTest {
}

fun main() {
    GlobalScope.launch(Dispatchers.IO
//            + CoroutineExceptionHandler { coroutineContext, throwable ->
//        println("GlobalScope.launch() handleException($coroutineContext, $throwable)")
//    }
) {
        launch(CoroutineExceptionHandler { coroutineContext, throwable ->
            println("launch() 0 handleException($coroutineContext, $throwable)")
        }) {
            delay(100)

//            throw Exception("Exception 1")
            println("launch() 0")
        }

        launch(CoroutineExceptionHandler { coroutineContext, throwable ->
            println("launch() 1 handleException($coroutineContext, $throwable)")
        }) {
            delay(200)

            println("launch() 1")

            throw Exception("Exception 1")
        }

        launch(CoroutineExceptionHandler { coroutineContext, throwable ->
            println("launch() 2 handleException($coroutineContext, $throwable)")
        }) {
            delay(300)

            println("launch() 2")
        }

        delay(100)

        println("GlobalScope.launch() delay(100)")

        delay(50)

        println("GlobalScope.launch() delay(50)")

        delay(100)

        println("GlobalScope.launch() 100")
    }

    try {
        Thread.sleep(100)
    } catch (e: InterruptedException) {
        e.printStackTrace()
    }

    println("main() 100ms")

    try {
        Thread.sleep(200)
    } catch (e: InterruptedException) {
        e.printStackTrace()
    }

    println("main() run 300ms")
}